<?php
namespace app\admins\controller;
use think\Controller;
use Util\data\Sysdb;



class Video extends BaseAdmin
{
	//影片列表
	public function index(){
		$data['pageSize'] = 2;
		$data['page'] = max(1,(int)input('get.page'));//前端获取page,默认为1
		$where=array();
		$data['find']=input('get.find');
		$data['find']&&$where= 'title like "%'.$data['find'].'%"';
		$data['data'] = $this->db->table('video')->where($where)->pages($data['pageSize']);
		$data['labels']=$this->db->table('admin_labels')->cates('id');
		

		$this->assign('data',$data);
		return $this->fetch();
	}


    //添加编辑影片
	public function add(){
		$data['channel']=$this->db->table('admin_labels')->where(array('flag'=>'channel'))->lists();
		$data['charge']=$this->db->table('admin_labels')->where(array('flag'=>'charge'))->lists();
		$data['area']=$this->db->table('admin_labels')->where(array('flag'=>'area'))->lists();

		$id=(int)input('get.id');
		$data['item']=$this->db->table('video')->where(array('id'=>$id))->item();

		$this->assign('data',$data);
		return $this->fetch();
	}


	
	//影片保存
	public function save(){
		$id=(int)input('post.id');
		$data['title']=trim(input('post.title'));
		$data['channel_id']=(int)input('post.channel_id');
		$data['charge_id']=(int)input('post.charge_id');
		$data['area_id']=(int)input('post.area_id');
		$data['img']=input('post.img');
		$data['keywords']=trim(input('post.keywords'));
		$data['url']=trim(input('post.url'));
		$data['desc']=trim(input('post.desc'));
		$data['status']=(int)input('post.status');
		if($id>0){
			//更新
			$this->db->table('video')->where(array('id'=>$id))->update($data);
		}else{
			//新增
			$data['add_time']=time();
			$this->db->table('video')->insert($data);
		}
		exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
	}


	//图片上传
	public function upload_img(){
		$file=request()->file('file');
		if($file==null){
			exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
		}
		$info=$file->move('../public/upload');
		$ext=$info->getExtension();//获取后缀名
		if(!in_array($ext,array('jpg','png','gif','jpeg'))){
			exit(json_encode(array('code'=>1,'msg'=>'文件格式不符合')));
		}
		$img='/upload/'.$info->getSaveName();
		exit(json_encode(array('code'=>0,'msg'=>$img)));
	}

	//删除影片
	public function delete(){
		$id=(int)input('post.id');
		$res=$this->db->table('video')->where(array('id'=>$id))->delete();
		if($res){
			exit(json_encode(array('code'=>0,'msg'=>'删除成功')));

		}
		


	}


}